package com.j180.demo.dao.student;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.j180.demo.bean.StudentBean;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IStudentDao extends BaseMapper<StudentBean> {

    /**
     * 添加学生
     * @param studentBean
     * @return
     */
    @Insert("insert into t_student(name,gender,age,classid,isdeleted,insertTime,updateTime) values (#{name},#{gender},#{age},#{classBean.id},0,#{insertTime},#{updateTime})")
    public int insertStudent(StudentBean studentBean);

    /**
     * 更新学生信息
     * @param studentBean
     * @return
     */
    @Update("update t_student set name=#{name},gender=#{gender},age=#{age},classid=#{classBean.id},updateTime=#{updateTime} where id=#{id}")
    public int updateStudent(StudentBean studentBean);

    /**
     * 根据条件分页查询学会生了个信息
     * @param page  分页
     * @param wrapper  条件构造器
     * @return 学生信息的集合
     */
    @Select("select stu.id as stuId,\n" +
            "       stu.name as stuName,\n" +
            "       stu.gender as stuGender,\n" +
            "       stu.age as stuAge,\n" +
            "       cls.id as clsId,\n" +
            "       cls.name as clsName\n" +
            "       from  t_student as stu \n" +
            "         left join t_class as cls on stu.classid = cls.id ${ew.customSqlSegment}")
    @ResultMap("resultMaps.stuMap")
    public Page<StudentBean> getAllStudentsByPage(Page page, @Param(Constants.WRAPPER) QueryWrapper wrapper);

    /**
     * 根据id查询学生详细信息
     * @param studentId  学生的编号
     * @return 学生详细对象
     */
    @Select("select stu.id as stuId,\n" +
            "       stu.name as stuName,\n" +
            "       stu.gender as stuGender,\n" +
            "       stu.age as stuAge,\n" +
            "       cls.id as clsId,\n" +
            "       cls.name as clsName\n" +
            "       from  t_student as stu \n" +
            "         left join t_class as cls on stu.classid = cls.id" +
            "       where stu.id=#{stduetId} and isdeleted=0")
    @ResultMap("resultMaps.stuMap")
    public StudentBean  getAllStudentsById(@Param("stduetId")int studentId);


}
